package com.example.coffeeseller_2.model.helper.view_helper;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.coffeeseller_2.R;
import com.example.coffeeseller_2.model.helper.TradeAnimManager;

/**
 * Created by WH on 2018/8/7.
 * 购买过程中右侧静态界面加载类
 */

public class StaticViewHelper {

    private View view;
    private Context context;
    ImageView loading_1;
    ImageView loading_2;
    ImageView loading_3;
    ImageView loading_4;


    public StaticViewHelper(Context context) {
        this.context = context;
        view = LayoutInflater.from(context).inflate(R.layout.include_loading_data, null, false);
    }

    private void startAnim() {

        if (loading_1 == null && loading_2 == null && loading_3 == null && loading_4 == null) {
            return;
        }
        TradeAnimManager.startWaitRotateAnim(loading_1, 0, 360);
        TradeAnimManager.startWaitRotateAnim(loading_2, 0, 360);
        TradeAnimManager.startWaitRotateAnim(loading_3, 0, 360);
        TradeAnimManager.startWaitRotateAnim(loading_4, 0, -360);
    }

    private void clearAnim() {

        if (loading_1 != null) loading_1.clearAnimation();
        if (loading_2 != null) loading_2.clearAnimation();
        if (loading_3 != null) loading_3.clearAnimation();
        if (loading_4 != null) loading_4.clearAnimation();
    }

    public void clear() {
        clearAnim();
    }

    private void clearAnim(View view) {

        if (view == null) return;
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View childView = ((ViewGroup) view).getChildAt(i);
            childView.clearAnimation();
        }
    }

    /*
    * 数据加载中
    * */
    public View getLoadingView() {
        clearAnim();
        view = LayoutInflater.from(context).inflate(R.layout.include_loading_data, null, false);
        loading_1 = (ImageView) view.findViewById(R.id.loading_1);
        loading_2 = (ImageView) view.findViewById(R.id.loading_2);
        loading_3 = (ImageView) view.findViewById(R.id.loading_3);
        loading_4 = (ImageView) view.findViewById(R.id.loading_4);
        startAnim();
        return view;
    }

    /*
        * 支付成功
        * */
    public View getPaySuccessView() {
        clearAnim();

        view = LayoutInflater.from(context).inflate(R.layout.include_pay_success, null, false);
        loading_1 = (ImageView) view.findViewById(R.id.loading_1);
        loading_2 = (ImageView) view.findViewById(R.id.loading_2);
        loading_3 = (ImageView) view.findViewById(R.id.loading_3);
        loading_4 = (ImageView) view.findViewById(R.id.loading_4);

        startAnim();
        return view;
    }

    /*
    *制作完成
    * */
    public View getMakeFinishView() {
        clearAnim();

        view = LayoutInflater.from(context).inflate(R.layout.include_making_success, null, false);
        loading_1 = (ImageView) view.findViewById(R.id.loading_1);
        loading_2 = (ImageView) view.findViewById(R.id.loading_2);
        loading_3 = (ImageView) view.findViewById(R.id.loading_3);
        loading_4 = (ImageView) view.findViewById(R.id.loading_4);
        startAnim();

        return view;
    }

    /*
    * 取杯成功
    * */
    public View getTakeCupSuccessView() {
        clearAnim();

        view = LayoutInflater.from(context).inflate(R.layout.include_takecup_success, null, false);
        loading_1 = (ImageView) view.findViewById(R.id.loading_1);
        loading_2 = (ImageView) view.findViewById(R.id.loading_2);
        loading_3 = (ImageView) view.findViewById(R.id.loading_3);
        loading_4 = (ImageView) view.findViewById(R.id.loading_4);
        startAnim();

        return view;
    }

    /*
    *
    * 支付失败
    * */
    public View getPayFailureView() {
        clearAnim();

        view = LayoutInflater.from(context).inflate(R.layout.include_pay_failture, null, false);
        loading_1 = (ImageView) view.findViewById(R.id.loading_1);
        loading_2 = (ImageView) view.findViewById(R.id.loading_2);
        loading_3 = (ImageView) view.findViewById(R.id.loading_3);
        loading_4 = (ImageView) view.findViewById(R.id.loading_4);
        startAnim();

        return view;
    }

    public View getMakingFailedView() {
        clearAnim();
        view = LayoutInflater.from(context).inflate(R.layout.include_downcup_failure, null, false);
        loading_1 = (ImageView) view.findViewById(R.id.loading_1);
        loading_2 = (ImageView) view.findViewById(R.id.loading_2);
        loading_3 = (ImageView) view.findViewById(R.id.loading_3);
        loading_4 = (ImageView) view.findViewById(R.id.loading_4);
        startAnim();

        return view;
    }

    /*
    * 当前
    * */
    public View getView() {

        return view;
    }
}
